package dsx.dsxFrame;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;

import dsx.biz._BizLib;
import dsx.config.Config;
import dsx.config.ConfigItem;
import dsx.console.DsxConsole;
import dsx.log.ServLog;
import dsx.monitor.central.Monitor;
import dsx.system.DsxClassLoader;

public class DsxFrame {

	static HashMap<String, RPCServer> serverMap = new HashMap<String, RPCServer>();

	// 初始化服务容器
	public static void init() throws Exception {
		DsxConsole.init();
		DsxClassLoader.init();
		loadServer();
		Monitor.init();
	}

	// 加载服务
	public static void loadServer() throws Exception {
		synchronized (serverMap) {
			ArrayList<_RPCServer> configList = loadServerConfig();
			for (_RPCServer config : configList) {
				if (serverMap.containsKey(config.getBizServerName())) {
					serverMap.get(config.getBizServerName()).init(config);
				} else {
					RPCServer rpcServer = new RPCServer();
					serverMap.put(config.getBizServerName(), rpcServer);
					rpcServer.init(config);
				}
			}
		}
	}

	// 加载服务配置
	private static ArrayList<_RPCServer> loadServerConfig() {
		ArrayList<_RPCServer> ret = new ArrayList<_RPCServer>();
		try {
			ArrayList<ArrayList<ConfigItem>> sections = Config
					.getSections("RPCServer");
			int moduleNum = sections.size();
			for (int i = 0; i < moduleNum; i++) {
				ArrayList<ConfigItem> fields = sections.get(i);
				_RPCServer config = new _RPCServer();
				for (int j = 0; j < fields.size(); j++) {
					ConfigItem field = fields.get(j);
					switch (field.name) {
					case "path":
						config.setPath(field.value);
						break;
					case "bizServerName":
						config.setBizServerName(field.value);
						break;
					case "netServerName":
						config.setNetServerName(field.value);
						break;
					case "bizDebugEnabled":
						if (field.value.equals("1"))
							config.setBizDebugEnabled(true);
						else
							config.setBizDebugEnabled(false);
						break;
					case "bizServerClass":
						config.setBizServerClass(field.value);
						break;
					case "converterClass":
						config.setConverterClass(field.value);
						break;
					case "netServerClass":
						config.setNetServerClass(field.value);
						break;
					case "slicerClass":
						config.setSlicerClass(field.value);
						break;
					case "maxWorkerNum":
						config.setMaxWorkerNum(Integer.parseInt(field.value));
						break;
					case "autoStart":
						if (field.value.equals("1"))
							config.setAutoStart(1);
						else
							config.setAutoStart(0);
						break;
					}
				}
				config.setNetServerConfig("");
				config.setStatus(0);
				ret.add(config);
			}
		} catch (Exception ex) {
			DsxConsole.out(ex.getMessage(), "system", ServLog.error);
		}
		return ret;
	}

	// 获取业务服务
	public static RPCServer getServer(String serverName) {
		synchronized (serverMap) {
			return serverMap.get(serverName);
		}
	}

	// 查询服务列表
	public static ArrayList<_RPCServer> getServerList() {
		synchronized (serverMap) {
			ArrayList<_RPCServer> serverList = new ArrayList<_RPCServer>();
			for (Entry<String, RPCServer> entry : serverMap.entrySet()) {
				serverList.add(entry.getValue().getConfig());
			}
			return serverList;
		}
	}

	// 查询业务模块列表
	public static ArrayList<_BizLib> getBizLibList(String serverName) {
		synchronized (serverMap) {
			RPCServer server = serverMap.get(serverName);
			if (server == null)
				return null;
			else
				return server.getBizLibList();
		}
	}

	// 启动全部服务
	public static void startAll() {
		synchronized (serverMap) {
			for (Entry<String, RPCServer> entry : serverMap.entrySet()) {
				entry.getValue().start();
			}
		}
	}

	// 关闭全部服务
	public static void stopAll() {
		synchronized (serverMap) {
			for (Entry<String, RPCServer> entry : serverMap.entrySet()) {
				entry.getValue().stop();
			}
		}
	}

	// 重启全部服务
	public static void restartAll() {
		synchronized (serverMap) {
			for (Entry<String, RPCServer> entry : serverMap.entrySet()) {
				entry.getValue().restart();
			}
		}
	}

	// 启动某个服务
	public static void start(String serverName) {
		synchronized (serverMap) {
			RPCServer server = serverMap.get(serverName);
			if (server != null)
				server.start();
		}
	}

	// 关闭某个服务
	public static void stop(String serverName) {
		synchronized (serverMap) {
			RPCServer server = serverMap.get(serverName);
			if (server != null)
				server.stop();
		}
	}

	// 重启某个服务
	public static void restart(String serverName) {
		synchronized (serverMap) {
			RPCServer server = serverMap.get(serverName);
			if (server != null)
				server.restart();
		}
	}

}
